package com.hqhsc.module.front.activity.dao;

import com.hqhsc.core.dao.BaseJdbcDaoImpl;
import com.hqhsc.module.common.dao.CommonDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class SignupDao extends BaseJdbcDaoImpl {
	final Logger logger = LoggerFactory.getLogger(SignupDao.class);


    @Autowired
    private CommonDao commonDao;

    public long getActivityUserByDn(long activity_id , String dn) {
        String sql = "select count(1) from activity_users where activity_id = ? and dn = ? ";
        Object[] o = new Object[]{ activity_id , dn };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().queryForLong(sql, o);
    }

    public int signupActivityUser(long activity_id , long channel_id , String user_name , String sex ,
                                   String dn , String id_card , String address , String budget ,
                                   String wedding_date , String marry_require , String other_require , String activity_name , String channel_name) {
        try{
            String sql = "insert into activity_users (name , sex , dn , id_card , address , budget , " +
                    "wedding_date , marry_require , other_require , activity_id , activity_name , " +
                    "channel_id , channel_name , is_import) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            Object[] o = new Object[]{ user_name , sex , dn , id_card , address , budget ,
                    wedding_date , marry_require , other_require , activity_id , activity_name ,
                    channel_id , channel_name , 0 };
            logger.info(sql.replaceAll("\\?", "{}"), o);
            return this.getJdbcTemplate().update(sql , o);
        } catch (Exception e) {
            logger.error(e.getMessage() , e);
            return  0;
        }
    }

    public int updateActivitySignupCount(long activity_id) {
        String sql = "update activity t set t.signup_count = signup_count + 1 where id = ?";
        Object[] o = new Object[]{ activity_id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql , o);
    }

    public int updateChannelSignupCount(long channel_id) {
        String sql = "update channel t set t.signup_count = signup_count + 1 where id = ?";
        Object[] o = new Object[]{ channel_id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql , o);
    }

    public int updateActivityChannelSignupCount(long activity_id , long channel_id) {
        String sql = "update activity_channel_detail set signup_count = signup_count + 1 where activity_id = ? and channel_id = ?";
        Object[] o = new Object[]{ activity_id , channel_id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql , o);
    }

    public int updateActivityPvUv(long activity_id , boolean is_update_pointcount , boolean is_update_uv) {
        String sql = "update activity t set t.pv = pv + 1 ";
        if(is_update_pointcount) {
            sql = sql + " , t.point_count = point_count + 1 ";
        }
        if(is_update_uv) {
            sql = sql + " , t.uv = uv + 1 ";
        }
        sql = sql + " where id = ?";
        Object[] o = new Object[]{ activity_id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql , o);
    }

    public int updateChannelPvUv(long channel_id , boolean is_update_pointcount , boolean is_update_uv) {
        String sql = "update channel t set t.pv = pv + 1 ";
        if(is_update_pointcount) {
            sql = sql + " , t.point_count = point_count + 1 ";
        }
        if(is_update_uv) {
            sql = sql + " , t.uv = uv + 1 ";
        }
        sql = sql + " where id = ?";
        Object[] o = new Object[]{ channel_id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql , o);
    }

    public int updateActivityChannelPvUv(long activity_id , long channel_id , boolean is_update_pointcount , boolean is_update_uv) {
        String sql = "update activity_channel_detail t set t.pv = pv + 1 ";
        if(is_update_pointcount) {
            sql = sql + " , t.point_count = point_count + 1";
        }
        if(is_update_uv) {
            sql = sql + " , t.uv = uv + 1 ";
        }
        sql = sql + " where activity_id = ? and channel_id = ?";
        Object[] o = new Object[]{ activity_id , channel_id };
        logger.info(sql.replaceAll("\\?", "{}"), o);
        return this.getJdbcTemplate().update(sql , o);
    }

}
